using LDNS.Interfaces;
using LDNS.SDK.Commands.Models;
using LDNS.Utilities;

namespace LDNS.Commands
{
    /// <summary>
    /// 清除缓存命令处理器
    /// 处理CLEAR_CACHE命令，清除DNS缓存
    /// 作者：huobing
    /// </summary>
    public class ClearCacheCommandHandler : CommandHandlerBase<ClearCacheCommand, ClearCacheResult>
    {
        private readonly IDnsService _dnsService;
        
        /// <summary>
        /// 命令名称
        /// </summary>
        public override string CommandName => ClearCacheCommand.NAME;
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dnsService">DNS服务实例</param>
        public ClearCacheCommandHandler(IDnsService dnsService)
        {
            _dnsService = dnsService;
        }
        
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="args">命令参数</param>
        /// <returns>执行结果</returns>
        public override BaseCommandResult Execute(string[] args)
        {
            try
            {
                // 使用接口中存在的方法来清空缓存
                // 清空所有上游DNS服务器的缓存
                // 传递null作为参数，让方法处理所有上游DNS服务器的缓存
                UpstreamDnsManager.Instance.ClearCacheByUpstreamDnsServers(null);
                return CreateSuccessResult("DNS缓存已清空");
            }
            catch (System.Exception ex)
            {
                return CreateErrorResult($"清空DNS缓存失败: {ex.Message}");
            }
        }
    }
}